import { createStore } from 'vuex'
import persist from 'vuex-persistedstate'
function formate() {
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let h = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s
}
export default createStore({
  state: {
    cur: 0,
    data: []
  },
  mutations: {
    changeCur(state, val) {
      state.cur = val
    },

    dataAdd(state, name) {
      let obj = {
        name: name.value,
        isExit: false,
        createTime: formate(),
        id: new Date().getTime(),
        state: state.cur // 定义状态为0的数据表示书籍 1表示电影 2 表示游戏 3表示综艺 4表示体育
      }
      let flag = false // 标记列表中是否已经存在该数据
      state.data.forEach(item => {
        if(item.name == name.value && item.state == state.cur) {
          alert('内容不能重复添加')
          flag = true
        } 
      })
      if(!flag) {
        state.data.unshift(obj)
      }
      
    },

    del(state, id) {
      state.data = state.data.filter(item => {
        return item.id != id
      })
    },


    changeData(state, arr) {
      state.data = arr
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
    persist()
  ]
})
